package com.programmercarl.hash;

import java.util.HashMap;

/**
 * https://leetcode.cn/problems/4sum-ii/description/
 *
 * @author yulongtian
 * @create 2024-12-14 19:00
 */
public class Test04 {
    public static void main(String[] args) {

    }

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer, Integer> map_sum_ab = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int temp_sum = nums1[i] + nums2[j];
                map_sum_ab.put(temp_sum, map_sum_ab.getOrDefault(temp_sum, 0) + 1);
            }
        }
        int count = 0;
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int temp_sum = nums3[i] + nums4[j];
                if (map_sum_ab.containsKey(-temp_sum)) {
                    count += map_sum_ab.get(-temp_sum);
                }
            }
        }
        return count;
    }

}
